﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WebBrowserTest.AnalyzeResults;
using HtmlAgilityPack;

namespace WebBrowserTest.Analyzers
{
    public class UniqueContentAnalyzer : AbstractAnalyzer
    {
        private HtmlDocument _htmlDoc = new HtmlDocument();
        public UniqueContentAnalyzer()
        {
            _lookupExpr = @"<(b|B)>(\d)+ result[s]?<(/b|/B)>";
        }

        public override AbstractAnalyzeResult GetAnalyseResult(string html, string url)
        {
           _htmlDoc.LoadHtml(html);
            return new UniqueContentResult {Date = DateTime.Now, DomainName = url,UniquePercentageValue = GetUniqueValue(html)};
        }

        private string GetUniqueValue(string pageContent)
        {
            var matches = Regex.Matches(pageContent, _lookupExpr);
            var pagesCount = 0;
            for (var i = 0; i < matches.Count; i++)
            {
               if(int.TryParse(matches[i].Groups[2].Value, out pagesCount))
               {
                   break;
               }
            }
            return matches.Count > 0 ? string.Format("{0:0.##} %", ((double) 1/(pagesCount + 1))*100) : "Brak danych";
        }
    }
}
